package com.tongyi.modules.mld.controller;

import com.google.common.collect.Lists;
import com.tongyi.annotation.IgnoreAuth;
import com.tongyi.common.annotation.SysLog;
import com.tongyi.common.utils.RestResponse;
import com.tongyi.modules.mld.controller.enums.SecondarySourceEnum;
import com.tongyi.modules.mld.controller.enums.SourceEnum;
import com.tongyi.modules.mld.controller.enums.StatusEnum;
import com.tongyi.modules.mld.controller.vo.MldOnlineReservationRequest;
import com.tongyi.modules.mld.entity.MldCustomReservationEntity;
import com.tongyi.modules.mld.service.IMldCustomReservationService;
import com.tongyi.modules.sys.entity.SysDictEntity;
import com.tongyi.modules.sys.service.SysDictService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class MldOnlineReservationController {

    @Autowired
    private IMldCustomReservationService iMldCustomReservationService;

    @Autowired
    private SysDictService sysDictService;

    /**
     * 根据主键删除
     *
     * @param ids
     * @return RestResponse
     */
    @SysLog("在线预约")
    @PostMapping("/online/reservation")
    @IgnoreAuth
    public RestResponse reservation(@RequestBody MldOnlineReservationRequest request) {
        //校验参数
        if (StringUtils.isBlank(request.getSource()) || StringUtils.isBlank(request.getCustomName()) || StringUtils.isBlank(request.getCustomPhone()) || StringUtils.isBlank(request.getDepartment())) {
            return RestResponse.error();
        }
        MldCustomReservationEntity entity = new MldCustomReservationEntity();
        entity.setCustomName(request.getCustomName());
        entity.setCustomPhone(request.getCustomPhone());
        List<SysDictEntity> sysDictEntities = sysDictService.queryByDistValues(Lists.newArrayList(request.getDepartment()));
        SourceEnum sourceEnum = SourceEnum.valueOf(request.getSource());
        entity.setSource(sourceEnum.name());
        if (StringUtils.isNotBlank(request.getSecondarySource())) {
            SecondarySourceEnum secondarySourceEnum = SecondarySourceEnum.valueOf(request.getSecondarySource());
            entity.setSecondarySource(secondarySourceEnum.name());
        }
        if (CollectionUtils.isEmpty(sysDictEntities)) {
            return RestResponse.error();
        }
        entity.setDepartment(sysDictEntities.get(0).getName());
        entity.setDepartmentCode(sysDictEntities.get(0).getValue());
        entity.setStatus(StatusEnum.init.name());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        iMldCustomReservationService.add(entity);
        return RestResponse.success();
    }

}
